﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Reflection;

namespace td.Core.DiscoverAddins
{
    public partial class RunTimeLists : DevExpress.XtraEditors.XtraUserControl
    {
        td.Core.Xml.AddIn addin;
        public RunTimeLists(ref td.Core.Xml.AddIn addin)
        {
            InitializeComponent();
            this.addin = addin;

            for (int i = 0; i < addin.Runtimes.Count; i++)
            {
                this.listBoxControl1.Items.Add(addin.Runtimes[i].AssemblyName);
            }
        }
        //添加按钮
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "dll文件|*.dll|exe文件|*.exe";
            if (open.ShowDialog() == DialogResult.OK)
            {
                var path = open.FileName;
                path = path.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "");
                if (Validation(path))
                {
                    this.listBoxControl1.Items.Add(path);
                    this.addin.Runtimes.Add(new td.Core.Xml.RuntimeXml(path,path));
                }
                else
                {
                    MessageBox.Show("你所选择的文件不包含插件！","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// 验证该文件是否包含插件
        /// </summary>
        /// <param name="asspath"></param>
        /// <returns></returns>
        private bool Validation(string asspath)
        {
            Assembly assembly = Assembly.LoadFrom(asspath);
            Type[] types = assembly.GetTypes();
            foreach (var t in types)
            {
                try
                {
                    var obj = assembly.CreateInstance(t.ToString());

                    if ((obj is td.Core.AbstractToolButtonCommand) || (obj is td.Core.AbstractViewContent) || (obj is td.Core.AbstractPad))
                    {
                        return true;
                    }
                }
                catch (Exception ee)
                {

                }
            }
            return false;
        }
        /// <summary>
        /// 验证按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            var r  =Validation( this.listBoxControl1.SelectedValue.ToString()).ToString();
            MessageBox.Show(r);
        }
        //删除按钮
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (this.listBoxControl1.SelectedItem != null)
            {
                addin.Runtimes.RemoveAt(this.listBoxControl1.SelectedIndex);
                UpDateListInfo();
            }
        }
        void UpDateListInfo()
        {
            this.listBoxControl1.Items.Clear();
            for (int i = 0; i < addin.Runtimes.Count; i++)
            {
                this.listBoxControl1.Items.Add(addin.Runtimes[i].AssemblyName);
            }
        }
    }
}
